package linhnv.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class PaseXMLHandler extends DefaultHandler{
    private List<HashMap<String, String>> myDataSet;
    private HashMap<String, String> currentItem; 
    private StringBuilder builder;
    //name element
    private String keyEndOfItem = "";
    //name of key in hashmap
    private String nameTagParent = "";
    //index of element duplicate in hashmap
    private int indexElemDuplicate = 1;
    //name of key in hashmap before current name key
    private String nameTagParentPre;
    //name of key in hashmap end of file XML
    private String nameTagParentEnd;
    
    /**
     * contrutor class
     * @param keyEndOfItem (name of element to split)
     */
    public PaseXMLHandler(String keyEndOfItem) {
		// TODO Auto-generated constructor stub
    	this.keyEndOfItem = keyEndOfItem;
	}
    
    
    /**
     * function get data after pase XML
     * @return List<Hashtable<String, String>>
     */
    public List<HashMap<String, String>> getData(){
        return this.myDataSet;
    }
    
    @Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.ignorableWhitespace(ch, start, length);
	}


	/**
     * get content of element (method of android)
     */
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        //get content of element
        builder.setLength(0);
        builder.append(ch, start, length);
    }

    /**
     * method android, end of element (tag)
     */
    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        super.endElement(uri, localName, name);
        //**** check duplicate key ***//
        if(builder.toString().trim().length()>0)
    	{
    		currentItem.put(nameTagParent, builder.toString());
    	}
        //if current element is element split then add to mDataSet
        if(localName.equals(keyEndOfItem))
        {
        	myDataSet.add(currentItem);
        	currentItem = new HashMap<String, String>();
        }
        if(localName.equals("brief"))
        {
        	ShowLog.showLogDebug("THUNA", "The brief is " + builder.toString());
        }
        builder = new StringBuilder();
        //get inndex name current element in string nameTagParaent
        int indexReplace = nameTagParent.lastIndexOf("."+localName);
        //remove name current element in string nametagParent
        if(nameTagParent != null && indexReplace > 0){
        	nameTagParentPre = nameTagParent;
        	nameTagParent = nameTagParent.substring(0, indexReplace);
        }        
    }

    /**
     * method android, when start read XML
     */
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        myDataSet = new ArrayList<HashMap<String, String>>();
        builder = new StringBuilder();
        currentItem = new HashMap<String, String>();
    }

    /**
     * method android, start element (tag)
     */
	@Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, name, attributes);
        
        nameTagParent = localName;
        
//        //check value nametagParent
//        if(nameTagParent != "")
//        {
//        	if(localName.equals(Constant.MAIN_TAG) || localName.equals(Constant.ROOT_TAG))
//        	{
//        		nameTagParent = localName;
//        	}
//        	nameTagParent += "." + localName;
//        }
//        else
//        {
//        	nameTagParent = localName;
//        }
//        
        
        //check list attribute
        if(attributes != null && attributes.getLength() > 0)
        {
        	Boolean flExit = false;
        	//**** get attribute and set to data from list attribute
	        for(int i=0; i< attributes.getLength(); i++)
	        {
	        	//key of value attribute
	        	String key  = nameTagParent + ".atbxml." + attributes.getLocalName(i);
	        	//check duplicate key, if exit then set value add more to old value 
	        	if(currentItem.get(key) != null && currentItem.get(key) != "")
	        	{
	        		key  = nameTagParent + indexElemDuplicate + ".atbxml." + attributes.getLocalName(i);
	        		currentItem.put(key, attributes.getValue(i));
	        		flExit = true;	        		
	        	}
	        	else
	        	{
	        		currentItem.put(key, attributes.getValue(i));
	        	}
	        }
	        
	        if(flExit)
	        {
	        	indexElemDuplicate++;
	        	nameTagParentEnd = nameTagParent;
	        }
	        else
	        {
	        	if(indexElemDuplicate > 1)
	        	{
	        		currentItem.put(nameTagParentPre+".total", String.valueOf(indexElemDuplicate));
	        	}
	        	indexElemDuplicate = 1;
	        }
        }
    }
	
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		myDataSet.add(currentItem);
		if(indexElemDuplicate > 1)
    	{
    		currentItem.put(nameTagParentEnd+".total", String.valueOf(indexElemDuplicate));
    	}
	}
}
